package com.imessage.action.business.othertask;

import com.bean.BusinessApple;
import com.bean.BusinessGroup;
import com.bean.BusinessTask;
import com.bean.BusinessTaskDetail;
import com.imessage.comm.Constants;
import com.imessage.type.BaseState;
import com.imessage.type.BusinessAppleState;
import com.imessage.utils.StrUtil;
import com.imessage.vo.AppleVo;
import com.imessage.vo.TaskVo;
import com.imessage.web.Page;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/admin/othertask")
public class OtherTaskAction extends OtherTaskBaseAction {


    /**
     * 列表数据,搜索数据
     */
    @RequestMapping(value = "")
    public Object run(TaskVo taskVo) {
        List<Expression> expressionList = new ArrayList<>();
        setExpressionList(expressionList, taskVo);
        Page<BusinessTaskDetail> page = baseService.findPage(BusinessTaskDetail.class, expressionList,
                null, getOrder(taskVo.getOrders()), taskVo.getPageCurrent(),
                taskVo.getPageSize());
        return page;
    }


    /**
     * 添加修改界面
     */
    @RequestMapping(value = "/initpage")
    public ModelAndView initpage(
            @RequestParam(name = "req_id", required = true) String id) {
        ModelAndView modelAndView = new ModelAndView();
        BusinessTaskDetail taskDetail = new BusinessTaskDetail();
        if (StringUtils.isNotBlank(id)) {
            taskDetail = baseService.findById(BusinessTaskDetail.class, id);
            modelAndView.setViewName("admin/business/othertask/modify");
        } else {
            modelAndView.setViewName("admin/business/othertask/add");
        }
        setModelAndView(modelAndView, taskDetail);
        return modelAndView;
    }





    /**
     * 修改
     */
    @RequestMapping(value = "/modify")
    public Object modify(TaskVo taskVo) {
        BusinessTaskDetail taskDetail = baseService.findById(BusinessTaskDetail.class, taskVo.getId());
        setTaskDetail(taskVo, taskDetail);
        int state = baseService.updateObject(taskDetail);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return taskDetail;
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del")
    public Object del(
            @RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            BusinessTaskDetail taskDetail = baseService.findById(BusinessTaskDetail.class, id);
            state = baseService.delObject(taskDetail);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }


    /**
     * 详情页
     */
    @RequestMapping(value = "/details")
    public ModelAndView details(String id) {
        ModelAndView modelAndView = new ModelAndView();
        BusinessTask task = baseService.findById(BusinessTask.class, id);
        modelAndView.setViewName("admin/business/task/details");
        modelAndView.addObject("movieForm", task);
        return modelAndView;
    }


}
